//
//  BGVisitorView.swift
//  WeiboSwift
//
//  Created by 战场原黑仪 on 16/7/19.
//  Copyright © 2016年 战场原黑仪. All rights reserved.
//

import UIKit
import SnapKit

class BGVisitorView: UIView {
    
    //声明一个闭包用于传递数据
    var closure: (() -> ())?
    
    //MARK: - 初始化
    //重写初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        setupLayer()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听方法
    @objc private func didClickLogin() {
        closure?()
    }
    
    func setupVisitorViewInfo(imageName: String?, title: String?) {
        //如果设置了图片和文字
        if let img = imageName, let text = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = text
            //隐藏背后的圆
            feedImageView.hidden = true
        }else {
            setupAnimation()
        }
    }
    
    //设置动画
    func setupAnimation() {
        //设置核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //旋转角度
        anim.toValue = M_PI * 2
        //动画时间
        anim.duration = 10
        //重复次数
        anim.repeatCount = MAXFLOAT
        //设置动画在切换到后台时不停
        anim.removedOnCompletion = false
        
        //为控件添加动画
        feedImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    //MARK: - 设置视图
    //添加控件
    func setupUI() {
        backgroundColor = Main_Color
        
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(regButton)
    }
    
    //添加约束
    func setupLayer() {
        
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 230, height: 35))
            make.top.equalTo(maskImageView.snp_bottom).offset(15)
            make.centerX.equalTo(self.snp_centerX)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.left.equalTo(messageLabel.snp_left)
            
        }
        
        regButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.right.equalTo(messageLabel.snp_right)
        }
        
    }
    
    
    //MARK: - 懒加载
    //圆圈
    private lazy var feedImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮照
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //中心图片
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text =  "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        label.font = Normal_Font
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.textColor = UIColor.grayColor()
        
        return label
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton()
        
        //基本属性
        btn.setTitle("登录", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = Normal_Font
        btn.setTitleColor(ThemeColor, forState: .Normal)
        
        //设置监听方法
        btn.addTarget(self, action: #selector(didClickLogin), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
    //注册按钮
    private lazy var regButton: UIButton = {
        let btn = UIButton()
        
        //基本属性
        btn.setTitle("注册", forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = Normal_Font
        btn.setTitleColor(ThemeColor, forState: .Normal)
        
        //设置监听方法
        btn.addTarget(self, action: #selector(didClickLogin), forControlEvents: .TouchUpInside)
        
        return btn
    }()
    
}
